<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            未上架退货
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">箱号</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">物料号</div>
                        <div class="aui-list-item-input">
                            <input type="text" id="mrlCode" disabled="true">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">批次号</div>
                        <div class="aui-list-item-input">
                            <input type="text" id="lotCode" disabled="true">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">数量</div>
                        <div class="aui-list-item-input">
                            <input type="number" id="qty" disabled="true">
                        </div>
                    </div>
                </li>

            </ul>
            <div style="width:100%;margin-top:2%;"></div>
            <div class="aui-btn aui-btn-info aui-btn-block" onclick="rejected()">确认退货</div>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称
    var initWorkCenterId = "";
    var scanRule = ""; //条码解析规则
    var sfkFlag = "N";
    var html = ""; //全局拼接HTML
    // var tbodyHtml1 = ""; //全局拼接TMID  的HTML表格
    var array = new Array();
    var arrSave = [];

    var delArray = new Array();
    var flag = true;
    var num = 0;
    var mrlnum = 0;
    var gcFlag = "Y"

    //初始化查询明细数据和下拉框下拉框
    apiready = function () {
        idSelector = "boxnum";
        //箱号选中
        $("#boxnum").select();
        //查询条码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#boxnum").blur();

                checkBoxNum(tm);
            }


        }
    });

    function rejected() {
        var boxnum = $("#boxnum").val();
        //校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '请扫描箱号！',
                duration: 3000
            });
            return;
        }


        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/boxnumRejected";
        var data = {
            values: {
                boxnum: boxnum, //箱号
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == "0") {
                    api.hideProgress();
                    api.toast({
                        msg: "退货成功",
                        duration: 3000
                    }); //清空表单
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    $("#boxnum").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    $("#lotCode").val("");
                    //失败
                } else if (myobj.code == "1") {
                    $("#boxnum").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    $("#lotCode").val("");
                    $("#boxnum").select();
                    idSelector = "boxnum";
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#boxnum").val("");
                $("#mrlCode").val("");
                $("#qty").val("");
                $("#lotCode").val("");
                $("#boxnum").select();
                idSelector = "boxnum";
                api.hideProgress();
                alert("连接超时，请耐心等待");
            }

        })
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //2:箱号
            if (idSelector == 'boxnum') {
                $("#boxnum").blur();
                checkBoxNum(ret);

            }
        });
    }

    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);
            $("#boxnum").val("");

        } else {

            //后台解析
            postAnalyze(sm);
            $("#boxnum").val("");

        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");

        var boxnum = "";
        var mrlCode = "";
        var lotCode = "";
        var qty = "";

        if (arr[1] == null || arr[1] == undefined) {
            mrlCode = "";
        } else {
            mrlCode = (arr[1]);
        }
        if (arr[2] == null || arr[2] == undefined) {
            qty = "";
        } else {
            qty = (arr[2]);
        }
        if (arr[3] == null || arr[3] == undefined) {
            lotCode = "";
        } else {
            lotCode = (arr[3]);
        }
        if (arr[9] == null || arr[9] == undefined) {
            boxnum = "";
        } else {
            boxnum = (arr[9]);
        }
        $("#boxnum").val(boxnum);
        $("#mrlCode").val(mrlCode);
        $("#lotCode").val(lotCode);
        $("#qty").val(qty);
    }


    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    if ($("#boxnum").val() == "") {
                        api.toast({
                            msg: "条码规则不正确，请重新扫码！",
                            duration: 3000
                        });
                        return;
                    }
                    //箱号选中
                    $("#boxnum").select();
                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#tm").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#boxnum").val("");
                $("#tm").val("");
                $("#boxnum").select();
                alert("连接超时，请耐心等待");
            }
        });
    }

    // 校验箱号
    function checkBoxNum(tm) {

        //查询归档弹框确认标识
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/checkBoxnumRejected";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                boxNum: tm, //箱号
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();

                    // 条码解析
                    tmSplit(tm);


                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#boxnum").val("");

                    api.setFocus({
                        inputId: 'boxnum',
                        focus: true
                    })


                }
            }
        });


    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>